<!DOCTYPE html>
<head>
    <title>用户编辑</title>
    {% include 'system/common/header.html' %}
</head>
<body>

<form class="layui-form" lay-filter="agricultural-form">
    <div class="layui-form-item">
        <input type="hidden" name="id" value="{{ area.id }}">
        <label class="layui-form-label">省份代码</label>
        <div class="layui-input-block">
            <input type="text" name="province_code" value="{{ area.province_code }}" required lay-verify="required" placeholder="请输入省份代码" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">省份名称</label>
        <div class="layui-input-block">
            <input type="text" name="province_name" value="{{ area.province_name }}" required lay-verify="required" placeholder="请输入省份名称" class="layui-input">
        </div>
    </div>


        <div class="layui-form-item">
        <label class="layui-form-label">年份</label>
        <div class="layui-input-block">
            <input type="text" name="year" value="{{ area.year }}" required lay-verify="required" placeholder="请输入省份名称" class="layui-input">
        </div>
    </div>


    <div class="layui-form-item">
        <label class="layui-form-label">受灾指标</label>
        <div class="layui-input-block">
            <input type="text" name="indicator" value="{{ area.indicator }}" required lay-verify="required" placeholder="请输入省份名称" class="layui-input">
        </div>
    </div>


        <div class="layui-form-item">
        <label class="layui-form-label">受灾面积（公顷）</label>
        <div class="layui-input-block">
            <input type="text" name="value" value="{{ area.value }}" required lay-verify="required" placeholder="请输入省份名称" class="layui-input">
        </div>
    </div>


       <div class="layui-form-item">
        <label class="layui-form-label">更新时间</label>
        <div class="layui-input-block">
            <input type="text" name="update_time" value="{{ area.update_time }}" required lay-verify="required" placeholder="请输入省份名称" class="layui-input">
        </div>

    </div>

    <!-- 省略其他字段的HTML结构，保持与add.html相同的表单布局 -->
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="update">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>
{% include 'system/common/footer.html' %}
<script>
layui.use(['form', 'jquery'], function() {
    const form = layui.form
        ,$ = layui.jquery;

    // 补充缺失的表单字段绑定
    form.val('agricultural-form', {
        'id': {{ area.id }},
        'province_code': '{{ area.province_code }}',
        'province_name': '{{ area.province_name }}',
        'year': '{{ area.year }}',
        'indicator': '{{ area.indicator }}',
        'value': '{{ area.value }}'
    });

    form.on('submit(update)', function(data) {
        // 修复ID字段传递（移除冗余赋值，依赖form.val已填充）
        $.ajax({
            url: '/agricultural/update',
            type: 'PUT',
            data: JSON.stringify(data.field), // 使用JSON格式与后端保持一致
            contentType: 'application/json', // 显式指定Content-Type
            success: function(res) {
                if (res.success) {
                    layer.msg(res.msg);
                    setTimeout(function() {
                        parent.layer.closeAll(); // 修改点：关闭当前层
                        parent.layui.table.reload('agricultural-table'); // 修改点：刷新表格而非重载页面
                    }, 1000);
                } else {
                    layer.msg(res.msg);
                }
            }
        });
        return false;
    });
});
</script>